<template>
    <theme-box title="好券速递添加">
        <form class="form-inline" @submit.prevent v-el:form>
            <div class="box-body">
                <form-select title="商户名称" :required="true" name="mchtId" :options="mchtList" :value.sync="mchtIdValue"></form-select>
                <form-select title="电子券名称" :required="true"  name="couponId" :options="coupList"></form-select>
                <form-select title="顺序" :required="true" name="seq" :options="seqOptions"></form-select>
            </div>
            <!-- /.box-body -->
            <div class="box-footer">
                <v-button type="reset" @click="reset">重置</v-button>
                <v-button type="submit" theme="primary" class="pull-right">确认</v-button>
            </div>
            <!-- /.box-footer -->
        </form>
    </theme-box>
</template>

<script>
    import 'plugins/jquery-validation';

    var serialize = require('form-serialize');


    export default {
        data: () => ({
            mchtIdValue:'', //商户值
            mchtList:[], //商户列表
            coupList:[],  //电子券列表
            seqOptions:[
                {key:"1",value:"1"},
                {key:"2",value:"2"},
                {key:"3",value:"3"},
                {key:"4",value:"4"}
            ]
        }),
        watch: {
            async mchtIdValue(v){
                if(v != ''){
                    await this.$post({
                        url: "/api/v2" + projectName_Url + "/plat/CouponExpress/queryCouponExpress",
                        data:{mchtId:v},
                        method:"POST"
                    }).then(response => {
                        let list = response.content.list;
                        if(list != '' && list != null){
                            this.$set("coupList", list);
                        }else{
                            this.coupList = []
                        }
                    }, error => {});
                }else{
                    await this.$post({
                        url: "/api/v2" + projectName_Url + "/plat/CouponExpress/queryCouponExpress",
                        data:{mchtId:""},
                        method:"POST"
                    }).then(response => {
                        let list = response.content.list;
                        if(list != '' && list != null){
                            this.$set("coupList", list);
                        }
                    }, error => {});
                }
            }
        },
        methods: {
            reset () {
                $("label.error").remove();
                this.$els.form.reset()
            },
            getMcht(data) {
                let self = this;
                var mchtId = JSON.parse(localStorage.getItem("id_token")).mchtId
                self.$post({
                    url: "/api/v2" + projectName_Url + "/list/mcht/"+ mchtId,
                    method:"GET"
                }).then(response => {
                    let list = response.content.info;
                    if(list != '' && list != [] && list != null){
                        self.$set("mchtList", list);

                    }
                }, error => {});
            },
        },
        ready () {
            var self = this;
            self.getMcht()
            $(this.$els.form).validate({
                submitHandler: async function () {
                    var _data = serialize(this.currentForm, {
                        hash: true,
                        empty: true
                    });
                    var result = await self.$post({
                        url: "/api/v2" + projectName_Url + "/plat/CouponExpress/add",
                        data: _data
                    })
                    let data = {
                        title: "操作成功",
                        content: "是否进入好券速递列表查看?"
                    }
                    Midway.confirm(data).then(function () {
                        self.$route.router.go('/couponDelivery/list');
                    })
                    self.reset()
                },

                errorPlacement: function (error, element) {
                    if (element.attr("name") == "file") {
                        error.insertAfter(".file-item");
                    } else {
                        error.insertAfter(element)
                    }
                },
                rules: {
                    mchtId:{
                        required:true,
                    },
                    couponId:{
                        required:true
                    }

                },
            });
        }
    }
</script>
